package com.hyd.dao.config;

import java.util.Collection;
import java.util.HashMap;

/**
 * 表示一个数据源。
 */
public class HydrogenDataSource {

    private HashMap<String, Connection> connections = new HashMap<String, Connection>();

    private String defaultConnectionName;

    private String name;

    private String description = "";

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @SuppressWarnings({"unchecked"})
    public Connection[] getConnections() {
        Collection values = connections.values();
        return (Connection[]) values.toArray(new Connection[values.size()]);
    }

    public void addConnection(Connection connection) {
        if (connection != null) {
            connections.put(connection.getName(), connection);
        }
    }

    public Connection getConnection(String name) throws ConfigErrorException {
        Connection conn = connections.get(name);
        if (conn != null) {
            return conn;
        } else {
            throw new ConfigErrorException("no configuration found for datasource \"" + name + "\"");
        }
    }

    public Connection getDefaultConnection() {
        return getConnection(getDefaultConnectionName());
    }

    public String getDefaultConnectionName() {
        return defaultConnectionName;
    }

    public void setDefaultConnectionName(String connection) {
        this.defaultConnectionName = connection;
    }

    public int size() {
        return connections.size();
    }

    public void clear() {
        connections.clear();
    }
}
